package com.java.laixiangdong.library.view;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class View {
    Scanner scanner = new Scanner(System.in);

    public void welcome() {
        System.out.println("欢迎使用XXXX图书管理系统");
    }

    public String inputLogin() {
        System.out.println("请输入用户名：");
        return scanner.nextLine();
    }

    public String inputPassword() {
        System.out.println("请输入密码：");
        return scanner.nextLine();
    }

    public int choose() {
        int n;
        System.out.println("请输入您要进行的操作：\n" +
                "1.图书新增\n" +
                "2.图书修改\n" +
                "3.图书删除\n" +
                "4.查找图书\n" +
                "5.所有图书\n" +
                "0.退出登陆");
        try {
            n = Integer.parseInt(scanner.nextLine());
        } catch (NumberFormatException e) {
            System.out.println("只能输入数字0-5");
            return choose();
        }
        if (n < 0 || n > 5) {
            System.out.println("只能输入数字0-5");
            return choose();
        }
        return n;
    }

    public String inputBookName() {
        System.out.println("请输入图书名称：");
        String s = scanner.nextLine();
        if (s == null || s.equals("")) {
            return inputBookName();
        }
        return s;
    }

    public String inputBookInfo() {
        System.out.println("请输入图书简介：");
        return scanner.nextLine();
    }

    public int inputBookPrice() {
        int n;
        System.out.println("请输入图书价格：");
        try {
            n = Integer.parseInt(scanner.nextLine());
        } catch (NumberFormatException e) {
            System.out.println("只能输入大于0的数字");
            return inputBookPrice();
        }
        if (n < 0) {
            System.out.println("只能输入大于0的数字");
            return inputBookPrice();
        }
        return n;
    }

    public Date inputBookPublishTime() {
        Date date;
        System.out.println("请输入出版日期，格式yyyy-MM-dd：");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = dateFormat.parse(scanner.nextLine());
        } catch (ParseException e) {
            return inputBookPublishTime();
        }
        return date;
    }

    public int inputSortFlag() {
        int n;
        System.out.println("请输入排序规则：\n" +
                "1.价格从高到低排序\n" +
                "2.价格从低到低排序\n" +
                "3.新旧排序");
        try {
            n = Integer.parseInt(scanner.nextLine());
        } catch (NumberFormatException e) {
            System.out.println("只能输入数字1-3");
            return inputSortFlag();
        }
        if (n < 1 || n > 3) {
            System.out.println("只能输入数字1-3");
            return inputSortFlag();
        }
        return n;
    }

}